﻿using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPIT.RentHouse.Service
{
    public class PermissionService : IPermissionService
    {
        /// <summary>
        /// 根据Id获取权限信息
        /// </summary>
        /// <param name="id">权限Id</param>
        /// <returns></returns>
        public PermissionDTO Get(long id)
        {
            RentHouseEntities db = new RentHouseEntities();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            var model = baseService.Get(a => a.Id == id);
            PermissionDTO permissionDTO = new PermissionDTO()
            {
                Id = model.Id,
                Description = model.Description,
                Name = model.Name
            };
            return permissionDTO;
        }
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <returns></returns>
        public List<PermissionDTO> GetList()
        {
            RentHouseEntities db = new RentHouseEntities();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            List<PermissionDTO> list = baseService.GetList(a => true).Select(a => new PermissionDTO()
            {
                Id = a.Id,
                Name = a.Name,
                Description = a.Description
            }).ToList();
            return list;
        }
        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<PermissionDTO> GetPagedList(int start, int length, string name,ref int count)
        {
            RentHouseEntities db = new RentHouseEntities();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            var query = PredicateExtensions.True<T_Permissions>();
            if (!string.IsNullOrWhiteSpace(name))
            {
                query= query.And(a => a.Name.Contains(name));
            }
            List<T_Permissions> list= baseService.GetPagedList(start, length, ref count, query, a => a.Id).ToList();
            return list.Select(a => new PermissionDTO()
            {
                Id = a.Id,
                Name = a.Name,
                Description = a.Description
            }).ToList();
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public AjaxResult Add(PermissionDTO dto)
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            T_Permissions per = new T_Permissions();
            per.Name = dto.Name;
            per.Description = dto.Description;
            per.CreateDateTime = DateTime.Now;
            long id = bs.Add(per);
            if (id > 0)
            {
                return new AjaxResult(ResultState.Success, "权限添加成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "权限添加失败！");
            }
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="permissionDTO"></param>
        /// <returns></returns>
        public AjaxResult Update(PermissionDTO permissionDTO)
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            var model = bs.Get(a => a.Id == permissionDTO.Id);
            model.Name = permissionDTO.Name;
            model.Description = permissionDTO.Description;
            bool res = bs.Update(model);
            if (res)
            {
                return new AjaxResult(ResultState.Success, "权限修改成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "权限修改失败！");
            }
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id">权限ID</param>
        /// <returns></returns>
        public AjaxResult Delete(long id)
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            var model = bs.Get(e => e.Id == id);
            if (bs.Delete(model))
            {
                return new AjaxResult(ResultState.Success, "删除权限成功");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "删除权限失败");
            }
        }

        /// <summary>
        /// 批量删除权限
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult DeleteBatch(List<long> ids)
        {
            try
            {
                var db = new RentHouseEntities();
                BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
                foreach (var id in ids)
                {
                    var model = bs.Get(e => e.Id == id);
                    bs.Delete(model);
                }
                return new AjaxResult(ResultState.Success, "删除成功");
            }
            catch (Exception)
            {
                return new AjaxResult(ResultState.Error, "删除失败");
            }
        }

    }
}
